import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';
import { formatDate } from '@core/util/date-utils';

/**
 * 自动发放奖金 服务
 */
@Injectable()
export class TriggerService extends BaseService {


  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/marketRedpackTrigger';


  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any, triggerId:any): Promise<any> {
    let url = this.base_url + '/pagination';
    if (triggerId) {
      url = url + '/log';
      page['_filter_eq_triggerId'] = triggerId;
    }
    if (keyword) {
      page['_filter_like_remark'] = keyword;
    }
    page['_sort_by_'] = 'saveTime_desc';
    return this.post(url, page);
  }

  /**
   * 获取所有列表 按排序号 升序排序
   * @returns {Promise<any>}
   */
  getAll(): Promise<any> {
    const url = this.base_url + '/all';
    return this.get(url);
  }

  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveOrUpdateEntity(id: any, entity: any): Promise<any> {
    if (entity.customerIds) {
      entity.customerIds = '[' + entity.customerIds.toString() + ']';
    }
    if (entity.beginTime) {
      entity.beginTime = formatDate(entity.beginTime, 'yyyy-MM-dd 00:00:00');
    }
    this.cacheService.setAgentLevel(null);
    if (id) {
      const url = this.base_url + '/' + id;
      return this.put(url, entity);
    } else {
      const url = this.base_url;
      return this.post(url, entity);
    }
  }

  /**
   * 进入奖金自动发放器页面需登录
   * @param entity
   * @returns {Promise<any>}
   */
  login(entity): Promise<any> {
    const url = this.base_url + '/actions/login';
    return this.post(url, entity);
  }
  /**
   * 奖金页面操作密码修改
   * @param entity
   * @returns {Promise<any>}
   */
  modifyPassword(password): Promise<any> {
    let url = this.base_url+"/actions/pwd/reset";
    return this.put(url,password)
  }


  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }

  /**
   * 商品上下架
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateStatus(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/'+id+'/status/' + entity.status;
    return this.put(url, entity);
  }



}
